<template>
  <div>
    <div class="search-header">
      <!-- 后退按钮 -->
      <van-icon
        name="arrow-left"
        color="white"
        size="18"
        class="goback"
        @click="$router.back()"
      />
      <!-- 搜索组件 -->
      <van-search
        placeholder="请输入搜索关键词"
        background="#007BFF"
        shape="round"
        v-fofo
        v-model="kw"
        @input="inputFn"
        @search="searchfn"
      />
    </div>
    <!-- 搜索建议列表 -->
    <div class="sugg-list" v-if="this.kw.length>0">
      <div class="sugg-item" v-for="str,index in suggestArr" :key="index" v-html="light(str)" @click="suggestFn(str)"></div>
    </div>
    <!-- 搜索历史----------------------------------------------------------------------------- -->
    <div class="search-history" v-else>
      <!-- 标题 -->
      <van-cell title="搜索历史">
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template #right-icon>
          <van-icon name="delete" class="search-icon" @click="deletFn"/>
        </template>
      </van-cell>
      <!-- 历史列表 -->
      <div class="history-list">
        <span class="history-item" v-for="item,index in history" :key="index" @click="clickhis(item)">{{
          item
        }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { setStorage, getStorage } from '@/utils/storage'
import { suggestAPI } from '@/api'
export default {
  data () {
    return {
      kw: '', // 搜索关键字
      timer: null,
      suggestArr: [],
      //   搜索历史初始
      history: JSON.parse(getStorage('his')) || []
    }
  },
  methods: {
    inputFn () {
      clearTimeout(this.timer)
      this.timer = setTimeout(async () => {
        const res = await suggestAPI({
          q: this.kw
        })
        this.suggestArr = res.data.data.options
        console.log(this.suggestArr)
      }, 500)
    },
    // 搜索列表文字高亮
    light (str) {
      const reg = new RegExp(this.kw, 'ig')
      return str.replace(reg, (match) => {
        return `<span style="color:red">${match}</span>`
      })
    },
    //
    searchfn () {
      // 把新的关键词缓存起来
      this.history.push(this.kw)
      this.history = Array.from(new Set(this.history))
      // 把搜索记录保存到本地
      setStorage('his', JSON.stringify(this.history))
      this.$router.push({
        path: `/search/${this.kw}`
      })
    },
    suggestFn (str) {
      this.history.push(str)
      this.history = Array.from(new Set(this.history))
      setStorage('his', JSON.stringify(this.history))
      this.$router.push({
        path: `/search/${str}`
      })
    },
    deletFn () {
      this.history = []
      setStorage('his', JSON.stringify([]))
    },
    clickhis (str) {
      this.$router.push({
        path: `/search/${str}`
      })
    }
  }
}
</script>

<style scoped lang="less">
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: #007bff;
  overflow: hidden;
  /*后退按钮*/
  .goback {
    padding-left: 14px;
  }
  /*搜索组件*/
  .van-search {
    flex: 1;
  }
}
/*搜索建议列表样式 */
.sugg-list {
  .sugg-item {
    padding: 0 15px;
    border-bottom: 1px solid #f8f8f8;
    font-size: 14px;
    line-height: 50px;
    // 实现省略号的三行代码
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.search-icon {
  font-size: 16px;
  line-height: inherit;
}
.history-list {
  padding: 0 10px;
  .history-item {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>
